{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组的合并与堆叠\n",
    "- 二维数组 向下（列）axis = 0  , 向右 （横） axis= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2],[3,4]])\n",
    "b = np.array([[5,6],[7,8]])\n",
    "print(np.concatenate((a, b), axis=0))\n",
    "print(np.concatenate((a, b), axis=1))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- numpy.stack\n",
    "- numpy.hstack\n",
    "- numpy.vstack "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1 2]\n",
      "  [3 4]]\n",
      "\n",
      " [[5 6]\n",
      "  [7 8]]]\n",
      "[[[1 2]\n",
      "  [5 6]]\n",
      "\n",
      " [[3 4]\n",
      "  [7 8]]]\n"
     ]
    }
   ],
   "source": [
    "print(np.stack((a, b), axis=0))\n",
    "print(np.stack((a, b), axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n"
     ]
    }
   ],
   "source": [
    "#水平堆叠 与 numpy.concatenate（（a,b）, axis=1）效果一样\n",
    "print(np.hstack((a,b)))\n",
    "#垂直堆叠 与 numpy.concatenate（（a,b）, axis=0）效果一样\n",
    "print(np.vstack((a,b)))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组的分割、追加、插入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([12, 23, 34, 45]), array([56, 67, 78, 89]), array([100, 110, 120, 130])]\n",
      "[array([12, 23, 34, 45]), array([56, 67, 78]), array([ 89, 100, 110, 120, 130])]\n"
     ]
    }
   ],
   "source": [
    "arr_split = np.array([12, 23, 34, 45, 56, 67, 78, 89, 100, 110, 120, 130])\n",
    "\n",
    "# 对等分割，无法等分时报错\n",
    "print(np.split(arr_split, 3))\n",
    "\n",
    "# 按索引位置分割，第一个索引4，数字到56不包含56，第二个索引7，数字到89不包含89\n",
    "print(np.split(arr_split, [4,7]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 12,  23],\n",
       "       [ 34,  45],\n",
       "       [ 56,  67],\n",
       "       [ 78,  89],\n",
       "       [100, 110],\n",
       "       [120, 130]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_split_2 = arr_split.reshape(6,2)\n",
    "arr_split_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[12, 23],\n",
       "        [34, 45]]),\n",
       " array([[56, 67],\n",
       "        [78, 89]]),\n",
       " array([[100, 110],\n",
       "        [120, 130]])]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(arr_split_2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 12],\n",
       "        [ 34],\n",
       "        [ 56],\n",
       "        [ 78],\n",
       "        [100],\n",
       "        [120]]),\n",
       " array([[ 23],\n",
       "        [ 45],\n",
       "        [ 67],\n",
       "        [ 89],\n",
       "        [110],\n",
       "        [130]])]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 水平分割\n",
    "np.split(arr_split_2, 2, axis=1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组追加与插入\n",
    "- append\n",
    "- insert"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 12,  23],\n",
       "       [ 34,  45],\n",
       "       [ 56,  67],\n",
       "       [ 78,  89],\n",
       "       [100, 110],\n",
       "       [120, 130],\n",
       "       [  1,   2]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.append(arr_split_2, [[1, 2]], axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 12,  23,   1],\n",
       "       [ 34,  45,   2],\n",
       "       [ 56,  67,   3],\n",
       "       [ 78,  89,   4],\n",
       "       [100, 110,   5],\n",
       "       [120, 130,   6]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维数组从axis= 1方向，即右侧增加，必须与数组列长相等。否则报错\n",
    "np.append(arr_split_2, [[1], [2], [3], [4], [5], [6]], axis=1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- numpy.insert(arr, obj, values, axis)\n",
    "\n",
    "`obj 为插入位置的索引 `"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 12  23]\n",
      " [ 34  45]\n",
      " [ 56  67]\n",
      " [ 78  89]\n",
      " [100 110]\n",
      " [120 130]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 12,  23, 123, 456,  34,  45,  56,  67,  78,  89, 100, 110, 120,\n",
       "       130])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(arr_split_2)\n",
    "np.insert(arr_split_2, 2, [123, 456])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 12,  23],\n",
       "       [ 34,  45],\n",
       "       [123, 456],\n",
       "       [ 56,  67],\n",
       "       [ 78,  89],\n",
       "       [100, 110],\n",
       "       [120, 130]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.insert(arr_split_2, 2, [123, 456], axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 12,  23],\n",
       "       [ 34,  45],\n",
       "       [123, 123],\n",
       "       [ 56,  67],\n",
       "       [ 78,  89],\n",
       "       [100, 110],\n",
       "       [120, 130]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 广播规则进行传播补齐\n",
    "np.insert(arr_split_2, 2, [123], axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 12,  23,   1],\n",
       "       [ 34,  45,   1],\n",
       "       [ 56,  67,   1],\n",
       "       [ 78,  89,   1],\n",
       "       [100, 110,   1],\n",
       "       [120, 130,   1]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.insert(arr_split_2, 2, [1], axis=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.8"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "6fff98fc3b3d81bd655c2cc48858186e4d9e2db7b515bf1c3221888f12a62f87"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
